<?php

if (! class_exists("Artsopolist_Calendar_Admin")) {
    
    class Artsopolist_Calendar_Admin extends Artsopolis_Calendar_API {
        
        public static function init() {
            add_action('admin_menu', array(__CLASS__, 'ac_admin_menu_plugin'));
            add_action('admin_init', array(__CLASS__, 'ac_register_options'));
            add_action('admin_init', array(__CLASS__, 'register_scripts'));
            add_action('admin_footer', array(__CLASS__, 'print_scripts'));
            add_action('admin_print_scripts', array(__CLASS__, 'do_jslibs'));
            
            add_action('wp_ajax_ac_check_valid_feed_url', array(__CLASS__, 'ac_check_valid_feed_url'));
            add_action('wp_ajax_ac_check_valid_category_xml_url', array(__CLASS__, 'ac_check_valid_category_xml_url'));
            
            add_action('wp_ajax_ac_get_territories', array(__CLASS__, 'ac_get_territories'));
            
            # Add setting API
            require ABSPATH. 'wp-admin/includes/template.php';
            add_action('admin_init', array ( __CLASS__, 'eg_settings_api_init' ));
        }
        
        # Add setting API
        public static function eg_settings_api_init() {
            
            // Add the section to reading settings so we can add our
            // fields to it
            add_settings_section(
                'artsopolis_calendar_reading_setting_section',
                'Example settings section in reading',
                array(__CLASS__, 'eg_setting_section_callback_function'),
                'reading'
            );

            // Add the field with the names and function to use for our new
            // settings, put it in our new section
            add_settings_field(
                'ac_reading_setting_api',
                'Ac setting api',
                array(__CLASS__, 'artsopolis_calendar_reading_setting_fields_function'),
                'reading',
                'artsopolis_calendar_reading_setting_section',
                array('ac_reading_setting_api')
            );
            
            add_settings_field(
                'ac_reading_setting_name',
                'Ac setting name',
                array(__CLASS__, 'artsopolis_calendar_reading_setting_fields_function'),
                'reading',
                'artsopolis_calendar_reading_setting_section',
                array('ac_reading_setting_name')
            );
            
            // Register our setting so that $_POST handling is done for us and
            // our callback function just has to echo the <input>
            register_setting( 
                'reading', 
                'ac_reading_setting_api', 
                array(__CLASS__, 'artsopolis_calendar_reading_settings_validation_api') 
            );
            
            register_setting(
                'reading', 
                'ac_reading_setting_name', 
                array(__CLASS__, 'artsopolis_calendar_reading_settings_validation_name')
            );
        }
        
        public static function artsopolis_calendar_reading_settings_validation_api ($input) {
            
            $value = get_option('ac_reading_setting_api');
            
            if (! $input) {
                add_settings_error(
                    'ac_reading_setting_name',
                    'error',
                    "Please enter your plugin name",
                    'error'
                );
                $input = $value;
            }
            return $input;
        }
        
        public static function artsopolis_calendar_reading_settings_validation_name ($input) {
            
            $value = get_option('ac_reading_setting_name');
                
            if (! $input) {
                add_settings_error(
                    'ac_reading_setting_api',
                    'error',
                    "Please enter your plugin api",
                    'error'
                );
                $input = $value;
            }
            return $input;
        }
        
        public static function eg_setting_section_callback_function() {
            echo '<p>Intro text for our settings section</p>';
        }
        
        public static function artsopolis_calendar_reading_setting_fields_function($args) {
            
            $value = get_option($args[0]); 
            echo '<input type="text" id="'.$args[0].'" name="'.$args[0].'" value="'.$value.'" />';
        }
        
        public static function ac_admin_menu_plugin() {
            // page title - menu title, capability, menu slug, function
            add_plugins_page('Artsopolis Calendar Options', 'Artsopolis Calendar', 'manage_options', 'admin-artsopolis-calendar', array(__CLASS__,  'ac_admin_plugin_options'));
        }

        public static function ac_admin_plugin_options() {
            
            if (! current_user_can('manage_options')) {
                wp_die(__('You do not have sufficient permissions access this page.'));
            }

            /*Define ajax url admin*/
            $admin_url = admin_url( 'admin-ajax.php' );
            $artsopolis_calendar_options = get_option('artsopolis_calendar_options');
            $category_opts = $artsopolis_calendar_options['category'];
            $arr_filters = Artsopolis_Calendar_Shortcode::$arr_filters;
            $arr_filter_settings = array(
                'Date',
                'Location'
            );
            
            self::$categories_url = $artsopolis_calendar_options['category_xml_feed_url'];
            
            $categories = self::get_categories();
          
            include dirname(__FILE__) . '/_admin_template.php';
        }

        /**
         * Register settings options 
         */
        public static function ac_register_options() {
            register_setting('artsopolis-calendar-group', 'artsopolis_calendar_options', array(__CLASS__, 'ac_validate_options'));
        }

        /**
         * Validate form
         */
        public static function ac_validate_options($input) {
            return $input;
        }
        
        /**
         * Function to register the javascript and style
         */
        public static function register_scripts () {
            wp_register_script('artsopolis-calendar-admin.js', plugins_url('artsopolis-calendar/js/artsopolis-calendar-admin.js'));
            wp_register_style('artsopolis-calendar.css', plugins_url('artsopolis-calendar/css/artsopolis-calendar.css'));
        }
        
        /**
         * Function to print the javascript and style registered
         */
        public static function print_scripts() {
            wp_print_scripts('artsopolis-calendar-admin.js');
            wp_print_styles('artsopolis-calendar.css');
        }
        
        public static function do_jslibs() {
            wp_enqueue_script('editor');
            wp_enqueue_script('thickbox');
            add_action('admin_head', 'wp_tiny_mce');
        }
        
        public static function ac_check_valid_feed_url() {
            if (! isset($_REQUEST['feed_url']) && ! isset($_REQUEST['artsopolis_calendar_options[feed_url]'])) {
                echo 0;
                exit;
            }
            
            if (defined( 'DOING_AJAX' ) && DOING_AJAX ) {
                self::$feed_url = $_REQUEST['feed_url'];
            } else {
                self::$feed_url = $_REQUEST['artsopolis_calendar_options[feed_url]'];
            }
            
            $xml = @simplexml_load_string(self::get_request_content(self::$feed_url));
            $events = @$xml->xpath('event');
            echo count($events) > 0 ? 1 : 0;
            exit;
        }
        
        public function ac_check_valid_category_xml_url() {
            if (! isset($_REQUEST['category_xml_feed_url']) && ! isset($_REQUEST['artsopolis_calendar_options[category_xml_feed_url]'])) {
                echo 0;
                exit;
            }
            
            if (defined( 'DOING_AJAX' ) && DOING_AJAX ) {
                self::$categories_url = $_REQUEST['category_xml_feed_url'];
            } else {
                self::$categories_url = $_REQUEST['artsopolis_calendar_options[category_xml_feed_url]'];
            }
           
            $categories = self::get_categories();
            
            if (empty($categories)) {
                exit('');
            }
            
            $artsopolis_calendar_options = get_option('artsopolis_calendar_options');
            $category_opts = $artsopolis_calendar_options['category'];
            
            // Render html and return
            ob_start();
            include dirname(__FILE__). '/_category-template.php';
            $html = ob_get_contents();
            ob_end_clean();
            
            echo $html;
            exit;
        }
        
        public static  function ac_get_territories() {
            exit;
        }
    }
    
    Artsopolist_Calendar_Admin::init();
}

